package review.system.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import review.config.Constants;
import review.entity.Users;
import review.system.service.UserService;

import java.util.List;

@Controller
@RequestMapping("/syatem")
@Api(tags = { "用户操作接口" })
public class UserAction {
	@Autowired
	private UserService userService;

	@ApiOperation(value = "添加用户", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/add_user", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object addUser(Users user) {
		JSONObject obj = new JSONObject();
		try {
			userService.addUser(user);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, user);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "登录用户修改密码", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/change_pwd", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object changePwd(String oldPwd, String newPwd) {
		JSONObject obj = new JSONObject();
		try {
			userService.changePwd(oldPwd, newPwd);
			obj.put(Constants.SUCCESS, true);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "分页查询用户", notes = "", httpMethod = "GET")
	@RequestMapping(value = "/query_User", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object query(Users users, @RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "10") int size) {
		JSONObject obj = new JSONObject();
		try {
			Page<Users> user = userService.findAll(page, size, users);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, user);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "修改用户", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/update_User", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object update(Users users) {
		JSONObject obj = new JSONObject();
		try {
			userService.updateUser(users);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, users);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "添加绑定角色", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/add_binding", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object add(Users users, Integer[] pid) {
		JSONObject obj = new JSONObject();
		try {
			userService.userRole(users, pid);
			obj.put(Constants.SUCCESS, true);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "查询所有未被禁用的用户", notes = "", httpMethod = "GET")
	@RequestMapping(value = "/query_usable", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object query(Users users) {
		JSONObject obj = new JSONObject();
		try {
			List<Users> list = userService.findAllUser(users);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, list);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "禁用用户", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/forbidden_User", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object forbidden(Integer[] pid) {
		JSONObject obj = new JSONObject();
		try {
			userService.userForbidden(pid);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, pid);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}

	@ApiOperation(value = "恢复用户", notes = "", httpMethod = "POST")
	@RequestMapping(value = "/recover_User", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object recover(Integer[] pid) {
		JSONObject obj = new JSONObject();
		try {
			userService.userRecover(pid);
			obj.put(Constants.SUCCESS, true);
			obj.put(Constants.DATA, pid);
		} catch (Exception e) {
			obj.put(Constants.MESSAGE, e.getMessage());
			obj.put(Constants.SUCCESS, false);
		}
		return obj;
	}
}
